﻿using System;
using Newtonsoft.Json;
using Newtonsoft.Json.Linq;

namespace Eagles.DomainService.Core.Utility
{
    class IpAddress
    {
        /// <summary>
        /// 淘宝api
        /// </summary>
        /// <param name="strIp"></param>
        /// <returns></returns>
        public static string GetIpCitys(string strIp)
        {
            try
            {
                string Url = "http://ip.taobao.com/service/getIpInfo.php?ip=" + strIp + "";
                System.Net.WebRequest wReq = System.Net.WebRequest.Create(Url);
                wReq.Timeout = 2000;
                System.Net.WebResponse wResp = wReq.GetResponse();
                System.IO.Stream respStream = wResp.GetResponseStream();
                using (System.IO.StreamReader reader = new System.IO.StreamReader(respStream))
                {
                    string jsonText = reader.ReadToEnd();
                    JObject ja = (JObject)JsonConvert.DeserializeObject(jsonText);
                    if (ja["code"].ToString() == "0")
                    {
                        string c = ja["data"]["city"].ToString();
                        int ci = c.IndexOf('市');
                        if (ci != -1)
                        {
                            c = c.Remove(ci, 1);
                        }
                        return c;
                    }
                    else
                    {
                        return "";
                    }
                }
            }
            catch (Exception e)
            {
                return ("");
            }
        }
    }
}